﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Xml;
using System.Reflection;
using SEAM.Exception.Formatter;
using System.Diagnostics;

namespace SEAM.Exception.Publisher
{
    public class EMailPublisher : ExceptionPublisherBase
    {
        private string _host;
        private int _port;
        private bool _asynch = true;
        private string _from;
        private string _to;
        private string _cc;
        private string _bcc;

        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }
        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }
        public bool Asynch
        {
            get { return _asynch; }
            set { _asynch = value; }
        }
        public string From
        {
            get { return _from; }
            set { _from = value; }
        }
        public string To
        {
            get { return _to; }
            set { _to = value; }
        }
        public string Cc
        {
            get { return _cc; }
            set { _cc = value; }
        }
        public string Bcc
        {
            get { return _bcc; }
            set { _bcc = value; }
        }

        public EMailPublisher(XmlNode node)
            : base(node) 
        {
            Host = GetAttributeValue(node, "host", true);
            
            if (!int.TryParse(GetAttributeValue(node, "port", false), out _port))
                Port = 21;
            
            if (!bool.TryParse(GetAttributeValue(node, "asynch", false), out _asynch))
                Asynch = true;

            From = GetAttributeValue(node, "from", true);
            To = GetAttributeValue(node, "to", true);
            Cc = GetAttributeValue(node, "cc", false);
            Bcc = GetAttributeValue(node, "bcc", false);
        }

        public override bool PublishException(System.Exception ex, IDictionary<string, object> additionalInfo)
        {
            try
            {
                using (MailMessage outMsg = new MailMessage(From, To))
                {
                    if (Cc == null)
                        outMsg.CC.Add(Cc);
                    if (Bcc == null)
                        outMsg.Bcc.Add(Bcc);
                    outMsg.Subject = ex.Message;
                    outMsg.Body = GetFormattedMessage(ex, Format, additionalInfo);
                    outMsg.IsBodyHtml = (Format is HTMLExceptionFormatter);

                    SmtpClient smtpConn = new SmtpClient(Host, Port);
                    smtpConn.UseDefaultCredentials = true;
                    if (Asynch)
                        smtpConn.SendAsync(outMsg, null);
                    else
                        smtpConn.Send(outMsg);
                }
            }
            catch (System.Exception ex2)
            {
                Trace.TraceWarning(ex2.Message + Environment.NewLine + ex2.StackTrace);
                return false;
            }
            return true;
        }
    }

}
